﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceProcess;
using System.Text;
using TV4Home.Server.TVEInteractionLibrary;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;

namespace TV4Home.Server.CoreService
{
    public class CoreService : ServiceBase
    {
        private ServiceHost serviceHost = null;

        public CoreService()
        {
            this.ServiceName = "TV4Home Core Service";
        }

        protected override void OnStart(string[] args)
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            serviceHost = new ServiceHost(typeof(TVEInteraction));

            foreach (ServiceEndpoint se in serviceHost.Description.Endpoints)
            {
                if (se.Name == "JsonEndpoint")
                    se.Behaviors.Add(new WebHttpWithCustomExceptionHandling());
            }

            serviceHost.Open();
        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }
}
